using System.Runtime.CompilerServices;
using Awaiter;
using Nsb.Classes;
using jQueryApi;

namespace Yoshi.Clusters
{

    [IgnoreNamespace]
    public class EditBase : Actor
    {
        protected Await AwpIn;
        protected jQueryObject CancelBtn;
        protected jQueryObject OkBtn;
        protected jQueryObject Textarea;
        protected string Msg = "";

        protected void BuildCommon()
        {
            Textarea = jQuery.FromHtml("<textarea/>")
                .AppendTo(Element)
                .Plugin<Jewl>().FValueString(Msg)
                ;

            OkBtn = jQuery.FromHtml("<button class='Button OkBtn'/>")
                .AppendTo(Element)
                .Click(Ok)
                ;

            CancelBtn = jQuery.FromHtml("<button class='Button CancelBtn'/>")
                .AppendTo(Element)
                .Click(Abort)
                ;
        }

        protected void Cancel(jQueryEvent e)
        {
            CancelBtn.Focus().Click();
        }

        [AlternateSignature]
        protected extern void Ok();

        protected virtual void Ok(jQueryEvent e)
        {
            CancelEvent(e);
            AwpIn[PkgBase.ResultRx] = Textarea.Plugin<Jewl>().GetFValueString();
            Destroy();
        }

        protected void Abort(jQueryEvent e)
        {
            CancelEvent(e);
            AwpIn.Abort();
            Destroy();
        }

        protected virtual void Destroy()
        {
            EscapeKeyAction();
            ShiftRetKeyAction();
            new Await()
                .AddAw(FadeOutAw, 80)
                .AddAw(RemoveAw)
                .AddFn(Hider, false)
                .Commit(AwpIn);
        }
    
    }
}
